<template>
  <div class="app-container">
    <el-row :gutter="20" class="app-container">
      <!-- 功能按钮 -->
      <el-col :xs="24">
        <el-col :span="4">
          <!-- 这是一个空的栅格 用来占位置 -->
<!--          <el-col :span="2">-->
<!--            <div style="height: 62px"></div>-->
<!--          </el-col>-->

          <!--------------------------- 从这开始--------------------------->
          <el-input
            v-model="diseCatalogParams.name"
            placeholder="请输入分类别名"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleCatalogAdd"
              v-hasPermi="['dms:diseCatalog:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="catalogMultiple"
              @click="handleCatalogDelete"
              v-hasPermi="['dms:diseCatalog:remove']"
            >删除</el-button>
          </el-col>
        </el-col>
        <el-col :span="20" :push="3">
          <!-- 条件搜索 + 目录显示 刷新等 -->
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="catId" v-if="show">
              <el-input
                v-model="queryParams.catId"
                placeholder="请输入目录id"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="code">
              <el-input
                v-model="queryParams.code"
                placeholder="请输入疾病编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入疾病名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="icd">
              <el-input
                v-model="queryParams.icd"
                placeholder="请输入ICD编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <!-- 功能按钮 -->
          <el-row :gutter="10" class="mb8">
<!--            <el-tooltip class="item" effect="dark" content="刷新" placement="top">-->
<!--              <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>-->
<!--            </el-tooltip>-->
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['dms:dise:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['dms:dise:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['dms:dise:remove']"
              >删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['dms:dise:export']"
              >导出</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-form label-width="100px">
                <el-form-item label="当前选择目录">
                  <el-input v-model="diseCatalogName" placeholder="当前未选择目录"/>
                </el-form-item>
              </el-form>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </el-col>
      </el-col>
      <!-- 数据显示 -->
      <el-col :xs="24">
        <el-col :span="4">
          <div class="head-container">
            <el-table v-loading="loading"  :data="diseCatalogList" style=" width: 100%;"
                      @row-click="handleCatalogRowClick" @selection-change="handleCatalogSelectionChange">
              <el-table-column type="selection"  align="center"/>
              <el-table-column prop="id" label="id" h align="center" v-if="show">
              </el-table-column>
              <el-table-column prop="name" label="疾病分类目录" style="height: 55px" align="center">
              </el-table-column>
            </el-table>
          </div>
          <pagination
            v-show="diseCatalogTotal>0"
            :total="diseCatalogTotal"
            :page.sync="diseCatalogParams.pageNum"
            :limit.sync="diseCatalogParams.pageSize"
            @pagination="getDiseCatalogList"
          />
        </el-col>
        <el-col :span="20">


          <el-table v-loading="loading" :data="diseList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="目录id" align="center" prop="catId" />
            <el-table-column label="疾病编码" align="center" prop="code" />
            <el-table-column label="疾病名称" align="center" prop="name" />
            <el-table-column label="ICD编码" align="center" prop="icd" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['dms:dise:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['dms:dise:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />

        </el-col>
      </el-col>
    </el-row>

    <!-- 添加或修改诊断类型（疾病）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="目录id" prop="catId">
          <el-input v-model="form.catId" placeholder="请输入目录id" />
        </el-form-item>
        <el-form-item label="疾病编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入疾病编码" />
        </el-form-item>
        <el-form-item label="疾病名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入疾病名称" />
        </el-form-item>
        <el-form-item label="ICD编码" prop="icd">
          <el-input v-model="form.icd" placeholder="请输入ICD编码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改诊断类型（疾病）目录对话框 -->
    <el-dialog :title="catalogTitle" :visible.sync="catalogOpen" width="550px" append-to-body>
      <el-form ref="catalogForm" :model="catalogForm" :rules="rules" label-width="100px">
        <el-form-item label="疾病目录名" prop="name">
          <el-input v-model="catalogForm.name" placeholder="请输入疾病目录名"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCatalogForm">确 定</el-button>
        <el-button @click="catalogCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDise, getDise, delDise, addDise, updateDise, exportDise } from "@/api/dms/dise";
import {listDise_catalog,getDise_catalog,delDise_catalog,addDise_catalog,updateDise_catalog,exportDise_catalog} from "@/api/dms/dise_catalog";

export default {
  name: "Dise",
  data() {
    return {
      //设置隐藏列
      show: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      catalogIds: [],
      // 非单个禁用
      catalogSingle: true,
      // 非多个禁用
      catalogMultiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 目录总条数
      diseCatalogTotal: 0,
      // 诊断类型（疾病）表格数据
      diseList: [],
      // 诊断类型(疾病)目录表格数据
      diseCatalogList: [],
      // 疾病目录名称
      name: undefined,
      // 弹出层标题
      title: "",
      // 弹出层标题
      catalogTitle: "",
      // 是否显示弹出层
      open: false,
      // 是否目录显示弹出层
      catalogOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catId: null,
        code: null,
        name: null,
        icd: null,
      },
      // diseCatalog查询参数
      diseCatalogParams: {
        id: undefined,
        pageNum: 1,
        pageSize: 12,
        name: undefined,
      },
      diseCatalogName: undefined,
      diseCatalogId: undefined,
      // 表单参数
      form: {},
      // 目录表单参数
      catalogForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选目录
    'diseCatalogParams.name'(val) {
      this.getDiseCatalogList();
    }
  },
  created() {
    this.getList();
    this.getDiseCatalogList();
  },
  methods: {
    /** 查询诊断类型（疾病）列表 */
    getList() {
      this.loading = true;
      listDise(this.queryParams).then(response => {
        this.diseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询诊断类型(疾病)目录列表 */
    getDiseCatalogList() {
      this.loading = true;
      listDise_catalog(this.diseCatalogParams).then(response => {
        this.diseCatalogList = response.rows;
        this.diseCatalogTotal = response.total;
        this.loading = false;
      });
    },
    /** 目录表 点击行事件*/
    handleCatalogRowClick(data) {
      this.queryParams.catId = data.id;  //获得目录IdD
      this.diseCatalogName = data.name;    //获得目录名称
      this.getList();
    },
    /**
     * 对话框打开回调
     * */
    dialogOpened: function () {
      // 将目录id 加入
      this.form.catId = this.queryParams.catId;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 目录对话框取消按钮
    catalogCancel() {
      this.catalogOpen = false;
      this.catalogReset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        catId: null,
        code: null,
        name: null,
        icd: null,
        status: 0
      };
      this.resetForm("form");
    },
    // 目录表单重置
    catalogReset() {
      this.catalogForm = {
        name: null
      };
      this.resetForm("catalogForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 目录多选框选中数据
    handleCatalogSelectionChange(selection) {
      this.catalogIds = selection.map(item => item.id)
      this.catalogSingle = selection.length !== 1
      this.catalogMultiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加诊断类型（疾病）";
    },
    /** 目录新增按钮操作 */
    handleCatalogAdd() {
      this.catalogReset();
      this.catalogOpen = true;
      this.catalogTitle = "添加诊断类型(疾病)目录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDise(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改诊断类型（疾病）";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDise(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDise(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 目录表单提交按钮 */
    submitCatalogForm() {
      this.$refs["catalogForm"].validate(valid => {
        if (valid) {
          addDise_catalog(this.catalogForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess("新增成功");
              this.catalogOpen = false;
              this.getDiseCatalogList();
            }
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除诊断类型（疾病）编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDise(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 目录删除按钮操作 */
    handleCatalogDelete(row) {
      const ids = row.id || this.catalogIds;
      this.$confirm('是否确认删除诊断类型（疾病）目录编号为"' + ids + '"的数据项,<span style="color: red">注意包括目录下的疾病</span>?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: true   // 设置会识别标签
      }).then(function() {
        return delDise_catalog(ids);
      }).then(() => {
        this.getDiseCatalogList();
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有诊断类型（疾病）数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDise(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.catalogForm;
      this.$confirm('是否确认导出所有诊断类型（疾病）目录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportDise_catalog(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function() {});
    }
  }
};
</script>
